package com.cho3.net.base;

import android.accounts.NetworkErrorException;
import android.content.Context;

import com.blankj.utilcode.util.ToastUtils;

import com.cho3.net.bean.BaseEntity;
import com.google.gson.JsonSyntaxException;

import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author yemao
 * @date 2017/4/9
 * @description 写自己的代码, 让别人说去吧!
 */

public abstract class BaseObserver<T> implements Observer<BaseEntity<T>> {
    protected Context mContext;

    public BaseObserver(Context cxt) {
        this.mContext = cxt;
    }

    public BaseObserver() {

    }
    @Override
    public void onSubscribe(Disposable d) {
        onRequestStart();

    }

    @Override
    public void onNext(BaseEntity<T> tBaseEntity) {
        onRequestEnd();
        if (tBaseEntity.isSuccess()) {
            try {
                T t = tBaseEntity.getData();
                onSucceed(t);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            onCodeError(tBaseEntity.getResultCode(),tBaseEntity.getReason());
        }
    }

    @Override
    public void onError(Throwable e) {
//        Log.w(TAG, "onError: ", );这里可以打印错误信息
        onRequestEnd();
        try {
            if (e instanceof ConnectException
                    || e instanceof TimeoutException
                    || e instanceof NetworkErrorException
                    || e instanceof UnknownHostException) {
                onFailure(e, true);
            } else {
                onFailure(e, false);
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        if (e instanceof JsonSyntaxException) {
           // ToastUtils.showShort("数据异常，请稍后再试");
        } else if (!(e instanceof UnknownHostException)) {
           // ToastUtils.showShort("服务器异常，请稍后再试", Gravity.CENTER);
        } else if (e instanceof NetworkErrorException) {
           // ToastUtils.showShort("网络连接不可用，检查你的网络设置");
        }

    }

    @Override
    public void onComplete() {
    }

    /**
     * 返回成功
     *
     * @param t
     * @throws Exception
     */
    protected abstract void onSucceed(T t) ;

    /**
     * 返回成功了,但是code错误
     *
     * @param
     * @throws
     */
    protected void onCodeError(String error_code, String reason) {
        //ToastUtils.showShort(reason);
    }

    /**
     * 返回失败
     *
     * @param e
     * @param isNetWorkError 是否是网络错误
     * @throws Exception
     */
    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
        if (isNetWorkError) {
            ToastUtils.showShort("网络连接不可用，检查你的网络设置");
        } else {

        }
    }

    protected void onRequestStart() {
    }

    protected void onRequestEnd() {
      //  closeProgressDialog();
    }

//    public void showProgressDialog() {
//        ProgressDialog.show(mContext, false, "请稍后");
//    }
//
//    public void closeProgressDialog() {
//        ProgressDialog.cancle();
//    }

}
